package com.buka.runner;

import com.buka.pojo.Article;
import com.buka.service.ArticleService;
import com.buka.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class ViewCountRunner implements CommandLineRunner {
    @Autowired
    private ArticleService articleService;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public void run(String... args) throws Exception {
        List<Article> list = articleService.list();

        Map<String, Object> map = new HashMap<>();
        for (Article article : list) {
            map.put(article.getId().toString(), article.getViewCount());
        }
        redisUtil.hSet("article:viewCount", map);
    }
}
